package com.lst.controller;

import com.lst.pojo.EasyUIResult;
import com.lst.pojo.User;
import com.lst.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "users")
    public String toUser(){
        return "users";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public EasyUIResult list(@RequestParam("page")Integer pageNum,@RequestParam("rows")Integer pageSize){
        return  userService.pageQuery(pageNum,pageSize);
    }

    @RequestMapping(value="/page/{pageName}")
    public String toUserAdd(@PathVariable("pageName") String pageName){
        return pageName;
    }

    /**
     * 请求路径：/save
     * 方法返回值：{status：200}
     * 参数：User对象
     */
    @ResponseBody
    @RequestMapping("/save")
    public Map<String,Object> addUser(User user){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            boolean flag=userService.addUser(user);
            if (flag){
                map.put("status",200);
            }else {
                map.put("status",500);
            }
        } catch (Exception e) {
            map.put("status",500);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> deleteUser(@RequestParam("ids")String ids){
        Map<String,Object> map = new HashMap<String, Object>();
        String[] idStr = ids.split(",");

        try {
            boolean flag=userService.deleteUser(idStr);
            if (flag){
                map.put("status",200);
            }else {
                map.put("status",500);
            }
        } catch (Exception e) {
            map.put("status",500);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> editUser(User user){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            boolean flag=userService.editUser(user);
            if (flag){
                map.put("status",200);
            }else {
                map.put("status",500);
            }
        } catch (Exception e) {
            map.put("status",500);
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping(value="export/excel")
    public String exportExcel(@RequestParam("page")Integer pageNum, @RequestParam("rows")Integer pageSize, Model model){
        EasyUIResult easyUIResult = this.userService.pageQuery(pageNum, pageSize);
        model.addAttribute("userList", easyUIResult.getRows());
        return "userExcelView";
    }


}
